// pages/confirm/confirm.js
var common = require("../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose_nav: 0,
        date: "2018-10-18",
        data: [],
        page_sign: "my",
        clerk: [],
        isIPX: "",
        page: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.showShareMenu({
            menus: [ "shareAppMessage", "shareTimeline" ]
        });
        var isIPX = app.globalData.isIPX;
        this.setData({
            isIPX: isIPX
        });
        var that = this;
        that.setData({
            clerk: wx.getStorageSync("clerk")
        });
        this.getData();
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        let currentdate = year + seperator1 + month + seperator1 + strDate;
        this.setData({
            date: currentdate
        });
        this.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        let that = this;
        //判断路由是否是从confirm_detail返回
                const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
        if (currentPage.__displayReporter.showReferpagepath == "pages/confirm_detail/confirm_detail.html") {
            that.setData({
                page: 1,
                data: []
            });
            this.getData();
        }
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.getData();
    },
    /**
   * 用户点击右上角分享
   */
    // onShareAppMessage: function () {
    // },
    choose_nav: function(e) {
        var id = e.currentTarget.dataset.id;
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        that.setData({
            choose_nav: id,
            page: 1,
            data: []
        });
        that.getData();
    },
    bindDateChange: function(e) {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.request({
            endpoint: "My/waitpick/leader_wait",
            method: "post",
            data: {
                openid: openid,
                oneday: e.detail.value
            },
            success: res => {
                that.setData({
                    data: res.extra
                });
            }
        });
        this.setData({
            date: e.detail.value
        });
    },
    getData: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        let choose_nav = that.data.choose_nav;
        let page = that.data.page;
        common.request({
            endpoint: "My/waitpick/leader_wait",
            method: "post",
            data: {
                openid: openid,
                choose_nav: choose_nav,
                page: page,
                rows: 10
            },
            success: res => {
                var data = that.data.data;
                let data2 = page == 1 ? res.extra : data.concat(res.extra);
                if (res.extra.length > 0) {
                    that.setData({
                        data: data2
                    });
                    that.data.page = page + 1;
                }
            }
        });
    },
    confirm: function(e) {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        var date = e.currentTarget.dataset.date;
        wx.showModal({
            title: "提示",
            content: "是否确认收货",
            success: function(res) {
                if (res.cancel) {} else {
                    common.request({
                        // endpoint: 'My/waitpick/leader_confirm2',
                        endpoint: "My/waitpick/leader-confirm-picked",
                        method: "post",
                        data: {
                            openid: openid,
                            date: date
                        },
                        success: res => {
                            wx.showModal({
                                title: "提示",
                                content: "收货成功，您的团员会自动收到取货通知！",
                                showCancel: false,
                                success: function() {
                                    /**
                   * 2019.07.13 大仙陈
                   * 需求目标:用户点击确认取货，状态改变，但不刷新页面，数据停留在当前位置
                   */
                                    var data = that.data.data;
                                    //确认收货,遍历数据，将unreceived_num的值改为0
                                                                        data.forEach((item, idnex) => {
                                        if (item.created_at == e.currentTarget.dataset.date) {
                                            //找到确认收货的这天的数据
                                            //将unreceived_num的值改为0
                                            item.unreceived_num = 0;
                                        }
                                    });
                                    //渲染数据
                                                                        that.setData({
                                        data: data
                                    });
                                }
                            });
                        },
                        fail: res => {
                            if (res.code == 2) {
                                wx.showToast({
                                    title: res.message,
                                    icon: "none"
                                });
                            } else {
                                wx.showToast({
                                    title: "收货失败",
                                    icon: "none"
                                });
                            }
                            setTimeout(() => {
                                that.getData();
                            }, 500);
                        }
                    });
                }
            }
        });
    },
    scancode: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        wx.scanCode({
            success: res => {
                wx.navigateTo({
                    url: "/" + res.path,
                    success() {},
                    fail(err) {
                        wx.showModal({
                            title: "提示",
                            content: "请扫描正确的提货二维码"
                        });
                    }
                });
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: "请扫描正确的提货二维码",
                    showCancel: false
                });
            }
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长"
                });
            }
        });
    }
});